package labox.innovation.loginserver.gameserverpackets;

import java.security.GeneralSecurityException;
import java.security.interfaces.RSAPrivateKey;

import javax.crypto.Cipher;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;

/**
 * @author -Wooden-
 * 
 */
public class BlowFishKey extends ClientBasePacket {
	byte[] _key;

	protected static final Logger _log = LoggerFactory.getLogger(BlowFishKey.class.getName());

	/**
	 * @param decrypt
	 */
	public BlowFishKey(byte[] decrypt, RSAPrivateKey privateKey) {
		super(decrypt);
		int size = readInt();
		byte[] tempKey = readBytes(size);
		try {
			byte[] tempDecryptKey;
			Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
			rsaCipher.init(Cipher.DECRYPT_MODE, privateKey);
			tempDecryptKey = rsaCipher.doFinal(tempKey);
			// there are nulls before the key we must remove them
			int i = 0;
			int len = tempDecryptKey.length;
			for (; i < len; i++) {
				if (tempDecryptKey[i] != 0)
					break;
			}
			_key = new byte[len - i];
			System.arraycopy(tempDecryptKey, i, _key, 0, len - i);
		} catch (GeneralSecurityException e) {
			_log.error("Error While decrypting blowfish key (RSA)", Config.SERVER_ID, e);
			e.printStackTrace();
		}
		/*catch(IOException ioe)
		{
			//TODO: manage
		}*/

	}

	public byte[] getKey() {
		return _key;
	}

}
